﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNideFurnace : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }
        if (btnSave.CommandArgument == "")
        {
            tbl_siconide_furnace model = new tbl_siconide_furnace();
            model.Start_Date = txtStart_Date.Text.Trim().toDateTimeWithoutError();
            model.Operator_Loading = ddlOperator_Loading.SelectedValue;
            model.Finish_Date = txtFinish_Date.Text.Trim().toDateTimeWithoutError();
            model.Operator_Unloading = ddlOperator_Unloading.SelectedValue;
            model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
            model.N2_K_m3_start = Convert.ToDecimal(txtN2_K_m3_start.Text.Trim());
            model.Ar_Pressure_start = Convert.ToDecimal(txtAr_Pressure_start.Text.Trim());
            model.H2_Pressure_start = Convert.ToDecimal(txtH2_Pressure_start.Text.Trim());
            model.He_Pressure_start = Convert.ToDecimal(txtHe_Pressure_start.Text.Trim());
            model.N2_K_m3_end = Convert.ToDecimal(txtN2_K_m3_end.Text.Trim());
            model.Ar_Pressure_end = Convert.ToDecimal(txtAr_Pressure_end.Text.Trim());
            model.H2_Pressure_end = Convert.ToDecimal(txtH2_Pressure_end.Text.Trim());
            model.He_Pressure_end = Convert.ToDecimal(txtHe_Pressure_end.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_siconide_furnace bll = new BLL_tbl_siconide_furnace();
            try
            {


                
                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_siconide_furnace model = new tbl_siconide_furnace();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.Start_Date = txtStart_Date.Text.Trim().toDateTimeWithoutError();
            model.Operator_Loading = ddlOperator_Loading.SelectedValue;
            model.Finish_Date = txtFinish_Date.Text.Trim().toDateTimeWithoutError();
            model.Operator_Unloading = ddlOperator_Unloading.SelectedValue;
            model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
            model.N2_K_m3_start = Convert.ToDecimal(txtN2_K_m3_start.Text.Trim());
            model.Ar_Pressure_start = Convert.ToDecimal(txtAr_Pressure_start.Text.Trim());
            model.H2_Pressure_start = Convert.ToDecimal(txtH2_Pressure_start.Text.Trim());
            model.He_Pressure_start = Convert.ToDecimal(txtHe_Pressure_start.Text.Trim());
            model.N2_K_m3_end = Convert.ToDecimal(txtN2_K_m3_end.Text.Trim());
            model.Ar_Pressure_end = Convert.ToDecimal(txtAr_Pressure_end.Text.Trim());
            model.H2_Pressure_end = Convert.ToDecimal(txtH2_Pressure_end.Text.Trim());
            model.He_Pressure_end = Convert.ToDecimal(txtHe_Pressure_end.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_siconide_furnace bll = new BLL_tbl_siconide_furnace();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_siconide_furnace model = new tbl_siconide_furnace();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_siconide_furnace bll = new BLL_tbl_siconide_furnace();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_siconide_furnace bll = new BLL_tbl_siconide_furnace();
                List<tbl_siconide_furnace> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_siconide_furnace tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    txtStart_Date.Text = tbl.Start_Date.ToString("yyyy-MM-dd");
                    ddlOperator_Loading.SelectedValue = tbl.Operator_Loading;
                    txtFinish_Date.Text = tbl.Finish_Date.ToString("yyyy-MM-dd");
                    ddlOperator_Unloading.SelectedValue = tbl.Operator_Unloading;
                    txtCharge_Number.Text = tbl.Charge_Number.ToString();
                    txtN2_K_m3_start.Text = tbl.N2_K_m3_start.ToString();
                    txtAr_Pressure_start.Text = tbl.Ar_Pressure_start.ToString();
                    txtH2_Pressure_start.Text = tbl.H2_Pressure_start.ToString();
                    txtHe_Pressure_start.Text = tbl.He_Pressure_start.ToString();
                    txtN2_K_m3_end.Text = tbl.N2_K_m3_end.ToString();
                    txtAr_Pressure_end.Text = tbl.Ar_Pressure_end.ToString();
                    txtH2_Pressure_end.Text = tbl.H2_Pressure_end.ToString();
                    txtHe_Pressure_end.Text = tbl.He_Pressure_end.ToString();
                    txtRemark.Text = tbl.Remark;
                    

                }
            }
        }
    }



    protected void ddlOperator_Loading_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlOperator_Loading.Items.Insert(0, li);
    }
    protected void ddlOperator_Unloading_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
       ddlOperator_Unloading.Items.Insert(0, li);
    }

    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 
            if (txtStart_Date.Text == "") return "Please enter Start Date";
            if (ddlOperator_Loading.SelectedIndex == 0) return "Please select Operator Loading";
            if (txtFinish_Date.Text == "") return "Please enter Finish Date";
            if (ddlOperator_Unloading.SelectedIndex == 0) return "Please select Operator Unloading";            
            if (txtCharge_Number.Text == "") return "Please enter Charge Number";
            if (txtN2_K_m3_start.Text == "") return "Please enter N₂_ kNm³ Weight at Start";
            if (txtN2_K_m3_end.Text == "") return "Please enter N₂_ kNm³ Weight at End";
            if (txtAr_Pressure_start.Text == "") return "Please enter Ar_bar Weight at Start";
            if (txtAr_Pressure_end.Text == "") return "Please enter Ar_bar Weight at End";
            if (txtH2_Pressure_start.Text == "") return "Please enter H₂_ bar Weight at Start";
            if (txtH2_Pressure_end.Text == "") return "Please enter H₂_ bar Weight at End";
            if (txtHe_Pressure_start.Text == "") return "Please enter He_bar Weight at Start";
            if (txtHe_Pressure_end.Text == "") return "Please enter He_bar Weight at End";

            if (txtRemark.Text == "") return "Please enter Remark";

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion
}